﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;

namespace AMS.DataAccess.Filter
{
    /// <summary>
    /// 一个包含各个 Filter 域的 Filter 对象
    /// </summary>
    public abstract class BaseFilter
    {
        /// <summary>
        /// 检查这个 Filter 对象是否为空（不含有任何筛选项）
        /// </summary>
        /// <returns></returns>
        public virtual bool IsEmpty()
        {
            Type type = GetType();
            PropertyInfo[] properties = type.GetProperties(BindingFlags.Instance | BindingFlags.Public);
            foreach (PropertyInfo item in properties)
            {
                if (!item.PropertyType.IsSubclassOf(typeof(FilterField)))
                {
                    continue;
                }

                if (!CheckPropertyNullable(item))
                {
                    return false;
                }
            }

            return true;
        }

        /// <summary>
        /// 检查筛选属性是否为空
        /// </summary>
        /// <param name="property">属性</param>
        /// <returns></returns>
        protected virtual bool CheckPropertyNullable(PropertyInfo property)
        {
            return (null == property.GetValue(this, null));
        }
    }
}
